/*
 * 代号：凤凰
 * http://www.jphenix.org
 * 2014-06-13
 * V4.0
 */
package com.jphenix.webserver.api;

import com.jphenix.servlet.filter.BaseFilter;
import com.jphenix.standard.docs.ClassInfo;
import com.jphenix.standard.servlet.api.IHttpSession;
import com.jphenix.standard.servlet.api.IHttpSessionContext;
import com.jphenix.standard.servlet.api.IServletContext;
import com.jphenix.webserver.instancea.ServeConnection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Hashtable;


/**
 * HTTP会话上下文处理类
 * 
 * 2019-09-17 整理了代码格式
 * 2022-09-04 重构了内置Servlet容器，不依赖外部ServletApi包
 * 
 * @author 刘虻
 * 2007-3-6下午01:25:08
 */
@SuppressWarnings("deprecation")
@ClassInfo({"2022-09-04 21:58","HTTP会话上下文处理类"})
public class HttpSessionContextImpl extends Hashtable<String,Object> implements IHttpSessionContext {

	/**
	 * 版本标识
	 */
	private static final long serialVersionUID = 7583084570667535490L;

	/**
	 * 覆盖方法
	 * @author 刘虻
	 * 2007-9-30下午05:34:38
	 */
	@Override
    public Enumeration<String> getIds() {
		return keys();
	}

	/**
	 * 覆盖方法
	 * @author 刘虻
	 * 2007-9-30下午05:34:47
	 */
	@Override
    public IHttpSession getSession(String sessionId) {
		return (IHttpSession) get(sessionId);
	}


	/**
	 * 母鸡 
	 * @author 刘虻
	 * 2008-7-8下午06:33:00
	 * @param w 母鸡 
	 * @throws IOException 执行发生异常
	 */
	protected void save(Writer w) throws IOException {
		Enumeration<?> e = elements();
		while (e.hasMoreElements()) {
			((SessionImpl) e.nextElement()).save(w);
		}
	}

	/**
	 * 母鸡
	 * @author 刘虻
	 * 2008-7-8下午06:43:48
	 * @param br               母鸡
	 * @param inactiveInterval 母鸡
	 * @param servletContext   母鸡
	 * @param serveConn        交互通信类
	 * @param baseFilter       主过滤器
	 * @return                 母鸡
	 * @throws IOException     母鸡
	 */
	protected static HttpSessionContextImpl restore(
			  BufferedReader  br
			, int             inactiveInterval
			, IServletContext servletContext
			, ServeConnection serveConn
			, BaseFilter      baseFilter)
			throws IOException {
		HttpSessionContextImpl result = new HttpSessionContextImpl();
		IHttpSession session;
		while ((session = SessionImpl.restore(br, inactiveInterval, servletContext, result, serveConn, baseFilter)) != null) {
			if (!((SessionImpl)session).checkExpired()) {
				result.put(session.getId(), session);
			}
		}
		return result;
	}
}